package cn.shiwei.org.controller;

import cn.shiwei.basic.util.PageList;
import cn.shiwei.basic.util.RequestStatus;
import cn.shiwei.org.domain.DictionariesDetail;
import cn.shiwei.org.query.DictionariesDetailQuery;
import cn.shiwei.org.service.IDictionariesDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dictionaryDetail")
@Api(tags = "数据字典细节接口",description = "数据字典细节接口详细描述")
public class DictionariesDetailController {

    @Autowired
    private IDictionariesDetailService detailService;

    /**
     * 分页查询
     * @RequestBody : 前端的对象数据是封装到了请求体中, 从请求体中取数据用 @RequestBody注解
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "部门列表分页查询",notes = "需要传参 为高级查询的条件")
    private PageList<DictionariesDetail> queryPage(@RequestBody DictionariesDetailQuery query){
        System.out.println(query);
        System.out.println(query.getType().getId());
        return detailService.queryDate(query);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除删除",notes = "需要参数id")
    private RequestStatus delete(@PathVariable("id") Long id){
        try {
            detailService.remove(id);
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("删除失败");
        }
    }

    @PutMapping()
    @ApiOperation(value = "类型添加或修改",notes = "如果有id就是修改否则就是添加")
    private  RequestStatus addAndUpdate(@RequestBody DictionariesDetail dictionariesDetail){
        System.out.println(dictionariesDetail);
        try {
            if(dictionariesDetail.getId() == null || dictionariesDetail.getId() == 0){
                detailService.save(dictionariesDetail);
            } else {
                detailService.update(dictionariesDetail);
            }
            return RequestStatus.me();
        } catch (Exception e) {
            e.printStackTrace();
            return RequestStatus.me().setMessage("操作失败");
        }
    }

}
